package com.qinglei.recoup.longconn.entity;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 用户数据数据体
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Deprecated
public class UserDataBody extends BodyT {
    public static final int DATA_LENGTH = 63;
    
    /**
     * 帧数位 uint32
     */
    private long frameNo;
    /**
     * 呼吸相位数据
     */
    private int[] breathPhaseData;
    /**
     * 心跳相位数据
     */
    private int[] heartPhaseData;
    /**
     * 呼吸频率 次/分钟
     */
    private int breathBpm;
    /**
     * 心跳频率 次/分钟
     */
    private int heartBpm;
    /**
     * s1状态
     */
    private int s1;
    /**
     * s2状态
     */
    private int s2;
    /**
     * 呼吸深度
     */
    private int breathDepth;
    /**
     * 距离门位置
     */
    private int positionByDoor;
    /**
     * 距离门能量
     */
    private int energyByDoor;
    /**
     * 离床及其他异常状态
     */
    private int abnormal;
    /**
     * 离床时间（s）
     */
    private long outBedTime;
    private int maxValDb2;
    private int tabFlag;
    private int rcsMode;
    private int cmBreath;
    private int cmHeart;
    
    @Override
    public byte[] encode() {
        ByteBuf byteBuf = Unpooled.buffer(DATA_LENGTH);

        byteBuf.writeInt((int)this.getFrameNo());

        int[] breathPhaseData = this.getBreathPhaseData();
        for (int i = 0; i <10; i++) {
            byteBuf.writeShort(breathPhaseData[i]);
        }

        int [] heartPhaseData = this.getHeartPhaseData();
        for (int i = 0; i <10; i++) {
            byteBuf.writeShort(heartPhaseData[i]);
        }
        byteBuf.writeByte(this.getBreathBpm());
        byteBuf.writeByte(this.getHeartBpm());
        byteBuf.writeByte(this.getS1());
        byteBuf.writeByte(this.getS2());
        byteBuf.writeByte(this.getBreathDepth());
        byteBuf.writeByte(this.getPositionByDoor());
        byteBuf.writeByte(this.getEnergyByDoor());
        byteBuf.writeByte(this.getAbnormal());
        byteBuf.writeInt((int) this.getOutBedTime());
        byteBuf.writeByte(this.getMaxValDb2());
        byteBuf.writeByte(this.getTabFlag());
        byteBuf.writeByte(this.getRcsMode());
        byteBuf.writeShort(this.getCmBreath());
        byteBuf.writeShort(this.getCmHeart());

        return byteBuf.array();
    }

    @Override
    public void decode(byte[] body) {
        ByteBuf byteBuf = Unpooled.wrappedBuffer(body);

        this.setFrameNo(byteBuf.readUnsignedInt());

        int[] breathPhaseData = new int[10];
        for (int i = 0; i <10; i++) {
            breathPhaseData[i] = byteBuf.readUnsignedShort();
        }
        this.setBreathPhaseData(breathPhaseData);

        int [] heartPhaseData = new int[10];
        for (int i = 0; i <10; i++) {
            heartPhaseData[i] = byteBuf.readUnsignedShort();
        }
        this.setHeartPhaseData(heartPhaseData);

        this.setBreathBpm(byteBuf.readUnsignedByte());
        this.setHeartBpm(byteBuf.readUnsignedByte());
        this.setS1(byteBuf.readUnsignedByte());
        this.setS2(byteBuf.readUnsignedByte());
        this.setBreathDepth(byteBuf.readUnsignedByte());
        this.setPositionByDoor(byteBuf.readUnsignedByte());
        this.setEnergyByDoor(byteBuf.readUnsignedByte());
        this.setAbnormal(byteBuf.readUnsignedByte());
        this.setOutBedTime(byteBuf.readUnsignedInt());
        this.setMaxValDb2(byteBuf.readUnsignedByte());
        this.setTabFlag(byteBuf.readUnsignedByte());
        this.setRcsMode(byteBuf.readUnsignedByte());
        this.setCmBreath(byteBuf.readUnsignedShort());
        this.setCmHeart(byteBuf.readUnsignedShort());
    }
}
